<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginData"
      label-width="80px"
      class="login-form"
    >
      <h2 class="title">Login</h2>

      <!-- Username Input -->
      <el-form-item
        prop="username"
        label="Username"
      >
        <el-input v-model.trim="loginData.username" />
      </el-form-item>

      <!-- Password Input -->
      <el-form-item
        prop="password"
        label="Password"
      >
        <el-input
          v-model.trim="loginData.password"
          type="password"
        />
      </el-form-item>

      <!-- Login Button -->
      <el-button
        type="success"
        class="btn"
        @click="handleSubmit"
      >登录</el-button>
      <el-button
        type="success"
        plain
        class="btn"
        @click="centerDialogVisible = true"
      >注册</el-button>
    </el-form>

    <!-- Error Message -->
    <p
      v-if="errorMessage !== ''"
      style="color: red;"
    >{{ errorMessage }}</p>

    <el-dialog
      title="用户注册"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
      :before-close="handleClose"
    >
      <el-form
        :model="registerData"
        label-width="80px"
        class="demo-form-inline"
        :inline="true"
      >
        <el-form-item label="用户名">
          <el-input
            v-model="registerData.username"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="registerData.password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input
            v-model="registerData.realName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="电话">
          <el-input
            v-model="registerData.phone"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label="用户类别">
          <el-select
            v-model="registerData.rank"
            placeholder="请选择"
          >
            <el-option
              label="普通用户"
              value="0"
            />
            <el-option
              label="vip"
              value="1"
            />
            <el-option
              label="管理员"
              value="2"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleRegister"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '../../utils/request'
import Cookies from 'js-cookie'

export default {
  data() {
    return {
      loginData: {
        username: '',
        password: ''
      },
      registerData: {
        username: '', // 用户名
        password: '', // 密码
        phone: '', // 电话
        realName: '', // 真实姓名
        rank: 0 // 0：普通用户  1：vip  2：管理员
      },
      errorMessage: '', // Used to display any errors on the page,
      allUsers: [],
      centerDialogVisible: false
    }
  },
  methods: {
    handleSubmit() {
      if (this.validateInput()) {
        request({
          method: 'post',
          url: 'mall/user/login',
          data: this.loginData
        }).then((response) => {
          Cookies.set('token', response.data.jwtToken)
          const rank = response.data.rank
          this.$message({
            message: response.message,
            type: 'success'
          })
          if (rank == 2) {
            this.$router.push('/dashboard/dashboard')
          } else {
            this.$router.push('/home')
          }
        })
      }
    },

    validateInput() {
      const isValid = true // Replace with actual validation logic
      if (this.loginData.username == '' || this.loginData.username == null) {
        this.errorMessage = '用户名不能为空'
        isValid = false
        return
      }
      if (this.loginData.password == '' || this.loginData.password == null) {
        this.errorMessage = '密码不能为空'
        isValid = false
        return
      }
      // Reset error message when input changes or form submitted again
      if (!isValid) {
        this.errorMessage = ''
      }

      return isValid
    },
    async handleRegister() {
      if (this.registerData.username && this.registerData.rank && this.registerData.password) {
        await request({
          method: 'post',
          url: '/mall/user/register',
          data: this.registerData
        }).then((response) => {
          console.log(response.data)
          this.$message({
            message: '注册成功',
            type: 'success'
          })
        })

        this.centerDialogVisible = false
      } else {
        console.log(this.registerData)
        this.$message({
          message: '用户名、密码、用户类别均不能为空！！',
          type: 'error'
        })
      }
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.centerDialogVisible = false
          this.registerData = {
            // avater: '',  //头像图片地址
            username: '', // 用户名
            password: '', // 密码
            phone: '', // 电话
            realName: '', // 真实姓名
            rank: null // 0：普通用户  1：vip  2：管理员
          }
          done()
        })
        .catch(_ => { })
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 370px;
  margin: 12% auto;
}

.login-form {
  padding: 40px;
  border: 1px solid #ccc;
  background-color: #f9f9f9;
}

.title {
  text-align: center;
  font-size: 24px;
  color: #666;
}
.btn {
  width: 80px;
  margin: 0 30px;
}
</style>
  d 3
